package com.example.blog.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.blog.common.pojo.dataobject.BlogInfo;
import com.example.blog.common.pojo.request.AddBlogParam;
import com.example.blog.common.pojo.request.UpBlogParam;
import com.example.blog.common.pojo.response.BlogInfoResponse;
import com.example.blog.common.utils.BeanConver;
import com.example.blog.mapper.BlogInfoMapper;
import com.example.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
//impl:实现类
//BlogServiceImpl来实现这里的BlogService
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogMapper;

    //查询博客列表
    @Override
    public List<BlogInfoResponse> getList() {
        //查询数据库,可看Mybatis-Plus课件
        List<BlogInfo> blogInfos = blogMapper.selectList(new LambdaQueryWrapper<BlogInfo>()
                //eq是equal的意思
                .eq(BlogInfo::getDeleteFlag, 0).orderByDesc(BlogInfo::getId));
        //转换,循环遍历
        List<BlogInfoResponse> blogInfoResponses = blogInfos.stream().map(blogInfo -> BeanConver.trans(blogInfo))
               // //再把它变成一个集合
                .collect(Collectors.toList());
        return blogInfoResponses;
    }

    //查询博客详情
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = selectBlogById(blogId);
        return BeanConver.trans(blogInfo);

    }

    @Override
    //操作之前要转换对象,因为数据库操作的对象是BlogInfo
    public Boolean addBlog(AddBlogParam param) {
        //对象转换   AddBlogParam->BlogInfo
//        BlogInfo blogInfo =new BlogInfo();
        BlogInfo blogInfo=BeanConver.trans(param);
                               //源,目标
        BeanUtils.copyProperties(param,blogInfo);
        try{
            //插入
            int result = blogMapper.insert(blogInfo);
            if(result==1){
                return true;
            }
        }catch(Exception e){
            log.error("博客插入失败,e:{}",e);
        }

        return false;
    }

    @Override
    public Boolean updateBlog(UpBlogParam param) {
        //对象的转换
        BlogInfo blogInfo=BeanConver.trans(param);
        //更新博客
        return update(blogInfo);
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo=new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        return update(blogInfo);
    }

    //从上面拆出的部分
    public BlogInfo selectBlogById(Integer blogId) {
        return blogMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId).eq(BlogInfo::getDeleteFlag, 0));
    }


    //更新博客数据
    public Boolean update(BlogInfo blogInfo){
        try{
            Integer result =blogMapper.updateById(blogInfo);
            if(result==1){
                return true;
            }
        }catch(Exception e){
            log.error("更新博客失败,e",e);
        }
        return false;
    }


}
